package com.goldsprite.gdxcore.entities;

import com.badlogic.gdx.graphics.g2d.*;
import java.util.*;

import com.badlogic.gdx.graphics.Texture;
import com.goldsprite.gdxcore.utils.sorts.EntitySort;
import com.goldsprite.utils.math.Vector2;

public class AnimatorEntity extends Renderable {
	private final Texture animTextures;
	private final TextureRegion[][] regions;
	private final Map<String, Animation> animations = new HashMap<>();
	private Map<TextureRegion, TextureRegionFlipable> flipStatus = new HashMap<>();
	private Animation curAnimation;
	private float stateTime;
	private Vector2 position = new Vector2();
	private Vector2 tmp = new Vector2();
	private Vector2 texSize = new Vector2();
	private Vector2 size = new Vector2();
	private Vector2 scl = new Vector2(1, 1);
	public Vector2 bodyBound = new Vector2(1, 1);//身体大小
	public Vector2 bodySize = new Vector2(1, 1);//经过scl的实际身体大小
	public Vector2 footPivot = new Vector2(1, 1);//脚底锚点偏移
	public Vector2 footPivotOffset = new Vector2(1, 1);//经过scl的脚底锚点偏移
	
	public boolean flipX, flipY;
	
	public TmpBlock playerRect;
	public EntitySort<TmpBlock> entSorter;
	

	public AnimatorEntity(Texture animTextures, int frameWidth, int frameHeight) {
		this.animTextures = animTextures;
		//将源材质按素材宽高单位分割
		regions = TextureRegion.split(animTextures, frameWidth, frameHeight);
		for(int y=0;y<regions.length;y++)
			for(int x=0;x<regions[0].length;x++)
				flipStatus.put(regions[y][x], new TextureRegionFlipable(regions[y][x]));
		
		texSize.set(frameWidth, frameHeight);
	}
	

	public void setPlayerRect(TmpBlock playerRect){
		this.playerRect = playerRect;
	}
	public void setEntSorter(EntitySort<TmpBlock> entSorter){
		this.entSorter = entSorter;
	}

	
	public void setScl(float scl) { setScl(scl, scl); }
	public void setScl(float sclX, float sclY) {
		scl.set(sclX, sclY);
	}
	public Vector2 getScl(){
		return scl;
	}
	public Vector2 getSize() {
		return size.set(texSize.x * scl.x, texSize.y * scl.y);
	}
	public Vector2 getBodySize(){
		return bodySize.set(bodyBound.x * scl.x, bodyBound.y * scl.y);
	}
	public Vector2 getFootPivotOffset(){
		return footPivotOffset.set(footPivot.x * scl.x, footPivot.y * scl.y);
	}
	
	public void setCenterPosition(float cx, float cy) {
		Vector2 size = getSize();
		setPosition(cx - size.x / 2, cy - size.y / 2);
	}
	public void setPosition(float x, float y){
		position.set(x, y);
	}
	public Vector2 getPosition(){ return position; }

	public Vector2 getCenterPosition(){
		return tmp.set(getSize()).div(2).add(position);
	}

	//往动画组添加动画：指定动画状态名，要切割的材质行数索引与帧数，以及设置动画播放速率
	public void addAnimation(String name, int row, int frameCount, float frameDuration) {
		//按帧数创建动画帧组
		TextureRegion[] frames = new TextureRegion[frameCount];
		//从源regions目标行填充到动画帧组
		System.arraycopy(regions[row], 0, frames, 0, frameCount);
		//在动画组创建新的动画帧组并设置播放帧率
		animations.put(name, new Animation(frames, frameDuration));
	}

	//设置当前动画
	public void setCurAnimation(String name) {
		curAnimation = animations.get(name);
		stateTime = 0;//切换动画重置播放时间
	}
	public String getCurAnimName(){
		for(String k : animations.keySet()){
			if(animations.get(k) == curAnimation) return k;
		}
		return "";
	}
	public TextureRegion getCurFrame(){
		TextureRegion frame = curAnimation.getFrame(stateTime);
		flipStatus.get(frame).setFlip(flipX, flipY);
		return frame;
	}
	
	public void setFlip(boolean flipX, boolean flipY){
		this.flipX = flipX;
		this.flipY = flipY;
	}
	
	
	public void act(float delta){
		if (curAnimation == null) return;
		//步进动画时间
		stateTime += delta;
	}
	
	//渲染动画
	@Override
	public void render(Batch batch, float delta) {
		if (curAnimation == null) return;
		
		//获取当前帧
		TextureRegion frame = getCurFrame();
		//在画布绘制
		batch.draw(frame, position.x, position.y, getSize().x, getSize().y);
	}
	
	public class TextureRegionFlipable {
		public boolean flipX;
		public boolean flipY;
		public TextureRegion region;
		
		public TextureRegionFlipable(TextureRegion region){
			this.region = region;
		}
		
		public void setFlip(boolean x, boolean y){
			region.flip(flipX!=x, flipY!=y);
			flipX=x;flipY = y;
		}
	}
}
